package com.learn.rpc.version.version1;

import com.learn.rpc.common.AdminUser;
import com.learn.rpc.common.AdminUserServiceImpl;
import com.learn.rpc.common.FinalParam;
import com.learn.rpc.common.IAdminUserService;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author wangxing
 * @version 2020/8/15 7:03 Administrator
 */
public class RpcServer {
    static boolean running = true;

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(FinalParam.PORT);
        while (running){
            try {
                Socket accept = serverSocket.accept();
                process(accept);
                accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void process(Socket socket) throws IOException {
//        获取数据输入流
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
//        获取数据输出流
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
//        获取传递过来的id参数
        Integer id = dataInputStream.readInt();
//        根据参数进行数据调用
        IAdminUserService adminUserService = new AdminUserServiceImpl();
        AdminUser user = adminUserService.findAdminUserById(id);
//        进行数据远程输出
        dataOutputStream.writeInt(user.getId());
        dataOutputStream.writeUTF(user.getName());
        dataOutputStream.flush();

        dataOutputStream.close();
        dataInputStream.close();
    }

}
